import { Body, Controller, Post, HttpCode } from "@nestjs/common";

const ok = (data: any = null) => ({ code: 0, msg: "success", data });
const fail = (msg: string, code = -1) => ({ code, msg });

@Controller("/user-service/recharage")
export class RechargeController {
  @Post("/weixin")
  @HttpCode(200)
  weixin(@Body() body: any) {
    // 入参: { body, purpose, totalFee, tradeType, userCode, userId, openid? }
    const totalFee = Number(body?.totalFee);
    if (!Number.isFinite(totalFee) || totalFee < 0) return fail("参数错误");
    // 返回跳转链接
    return ok("https://pay.mock/wechat?orderId=123");
  }

  @Post("/alipay")
  @HttpCode(200)
  alipay(@Body() body: any) {
    const totalFee = Number(body?.totalFee);
    if (!Number.isFinite(totalFee) || totalFee < 0) return fail("参数错误");
    // 返回 form html
    const html =
      '<form id="pay" action="https://pay.mock/alipay" method="post"><input type="hidden" name="orderId" value="123"/></form>';
    return ok(html);
  }
}
